/*
 * File:   indexedList.cc
 * Author: Mist
 *
 * Created on October 1, 2009, 5:45 PM
 */

#include "indexedList.h"

//ListNode- Constructor
//
//Function- Initialize the item and key or string

ListNode::ListNode(void* itemPtr, int key){
    item = itemPtr;
    noKey = key;
    next = NULL;
}

ListNode::ListNode(void* itemPtr, char* key){
    item = itemPtr;
    // get the size of the sting key
    int len = strlen(key);
    // set the local nameKey ptr to that size
    nameKey = (char*)malloc(sizeof(char)*len);
    //copy string
    strcpy(nameKey,key);
    next = NULL;
}

//List- Constructor
//
//Function- initializes the list as the list is empty head and end will
//          be null
IndexList::IndexList(){
    head = NULL;
    end = NULL;
}

//Append- Constructor
//
//Function- Add the item at the end of the list

void IndexList::Append(void *itemPtr, int key){
    //Initialize a node and add information
    ListNode *node;
    node = new ListNode(itemPtr, key);

    //if the list is empty
    if(head==NULL){
        head = node;
        end = node;
    }
    //else add the item at the end
    else{
        end->next = node;
        end = node;
    }
    return;
}


void IndexList::Append(void *itemPtr, char* key){
    //Initialize a node and add information
    ListNode *node;
    node = new ListNode(itemPtr, key);

    //if the list is empty
    if(head==NULL){
        head = node;
        end = node;
    }
    //else add the item at the end
    else{
        end->next = node;
        end = node;
    }
    return;
}

//Append- Constructor
//
//Function- removes item based on index

int IndexList::Remove(int key){
    ListNode *tempNode;
    ListNode *prevNode;

    //move down from head to end to check for the key
    tempNode = head;
    prevNode = NULL;

    //continue the chain till you reach the end
    while(tempNode!=NULL){

       if(tempNode->noKey==key){
           //if the item to be removed is head
           if(prevNode == NULL){
               //make the head->next as head
               head = tempNode->next;
               //if the head next is null end is null
               if(head == NULL)
               end = NULL;
           }
           // if a standard internal node make the next of tempNode as
           // next of previous node
           else{
               prevNode->next = tempNode->next;
               delete tempNode;
           }
           //on success return
           return 1;
       }
       // move to the next node and keep track of previous node
       else{
           prevNode = tempNode;
           tempNode = tempNode->next;
       }
    }
    //on failure return zero
    return 0;
}

int IndexList::Remove(char* key){
    ListNode *tempNode;
    ListNode *prevNode;

    //move down from head to end to check for the key
    tempNode = head;
    prevNode = NULL;

    //continue the chain till you reach the end
    while(tempNode!=NULL){

       if(strcmp(tempNode->nameKey,key)==0){
           //if the item to be removed is head
           if(prevNode == NULL){
               //make the head->next as head
               head = tempNode->next;
               //if the head next is null end is null
               if(head == NULL)
               end = NULL;
           }
           // if a standard internal node make the next of tempNode as
           // next of previous node
           else{
               prevNode->next = tempNode->next;
               delete tempNode;
           }
           //on success return
           return 1;
       }
       // move to the next node and keep track of previous node
       else{
           prevNode = tempNode;
           tempNode = tempNode->next;
       }
    }
    //on failure return zero
    return 0;
}

void *IndexList::getReference(int key){
    ListNode *tempNode;

    //move down from head to end to check for the key
    tempNode = head;

    //continue the chain till you reach the end
    while(tempNode!=NULL){

       if(tempNode->noKey==key){
           //return the reference
           return tempNode->item;
       }
       // move to the next node and keep track of previous node
       else{

           tempNode = tempNode->next;
       }
    }
    //on failure return zero
    return NULL;
}

void *IndexList::getReference(char* key){
    ListNode *tempNode;

    //move down from head to end to check for the key
    tempNode = head;

    //continue the chain till you reach the end
    while(tempNode!=NULL){

       if(strcmp(tempNode->nameKey,key)==0){
           //return the reference
           return tempNode->item;
       }
       // move to the next node and keep track of previous node
       else{

           tempNode = tempNode->next;
       }
    }
    //on failure return zero
    return NULL;
}